package com.liliudong.template.config;

import com.liliudong.template.common.entity.BaseEntity;
import com.liliudong.template.common.listener.BaseFieldFillListener;
import com.liliudong.template.impl.DataPermissionDialectImpl;
import com.mybatisflex.core.FlexGlobalConfig;
import com.mybatisflex.core.dialect.DbType;
import com.mybatisflex.core.dialect.DialectFactory;
import com.mybatisflex.core.logicdelete.LogicDeleteProcessor;
import com.mybatisflex.core.logicdelete.impl.IntegerLogicDeleteProcessor;
import com.mybatisflex.spring.boot.MyBatisFlexCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MybatisFlexConfiguration implements MyBatisFlexCustomizer {

    /**
     * 逻辑删除配置
     *
     * @return
     */
    @Bean
    public LogicDeleteProcessor logicDeleteProcessor() {
        return new IntegerLogicDeleteProcessor();
    }

    @Override
    public void customize(FlexGlobalConfig flexGlobalConfig) {
        DialectFactory.registerDialect(DbType.MYSQL, new DataPermissionDialectImpl());

        // 全局数据自动填充监听器
        flexGlobalConfig.registerInsertListener(new BaseFieldFillListener(), BaseEntity.class);
        flexGlobalConfig.registerUpdateListener(new BaseFieldFillListener(), BaseEntity.class);
    }
}
